﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PKURG.CostSplit.Business;
using System.Text;

public partial class ProjectView : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    public void LoadData()
    {
        List<dynamic> infos = Project.GetProjectAllData(CurrentUserInfo.Name);

        if (infos.Count==0)
        {
            ProjectList.InnerHtml = "没有可显示项目";
            return;
        }

        StringBuilder finallyContent = new StringBuilder();

        //if (CurrentUserInfo.Ptype==0)//集团用户
        //{
        var companyInfos = infos.GroupBy(x => x.CompanyName).ToDictionary(x => x.Key, x => x.ToList());
        foreach (var item in companyInfos)
        {
            string grandParentNodeFormat = "<div class='gpn'><img src='/images/TVPLUS.GIF'/><span>{0}</span>{1}</div>";
            finallyContent.AppendFormat(grandParentNodeFormat, item.Key, GetProjectData(item.Value));
        }
        //}

        // List<dynamic> infos =Project.GetProjectAllData();



        ProjectList.InnerHtml = finallyContent.ToString();
    }

    private string GetProjectData(List<dynamic> list)
    {
        string parentNodeFormat = "<div class='pn' style='display:none;'><img src='/images/TVPLUS.GIF'/><span>{0}</span><div class='childDiv' style='display:none;'>{1}</div></div>";

        StringBuilder content = new StringBuilder();
        foreach (dynamic item in list)
        {
            content.AppendFormat(parentNodeFormat, item.PName, GetItemHtml(item.StageList));
        }

        return content.ToString();
    }
    private string GetItemHtml(List<dynamic> infos)
    {
        string id = Request["id"];
        string childNodeFormat = "<div class='cn'><span>{0}</span> {1}</div>";
        StringBuilder content = new StringBuilder();
        foreach (var item in infos)
        {
            string link = "";
            if (string.IsNullOrEmpty(id) || id == "1")
            {
                link = string.Format("<a target='_blank' href='StageArea.aspx?id={0}'>面积录入</a>", item.Id);
            }
            //link += string.Format("<a href='pages/StageSplit.aspx?id={0}'>已发生成本拆分</a>", item.Id);
            if (item.rCount != "0")
            {
                //link = string.Format("<a href='SCMain.aspx?id={0}'>开始拆分</a>", item.Id);
                //link = string.Format("<a href='#' onclick='OpenDig({0},0)'>已发生成本拆分</a>", item.Id) +
                //    string.Format("<a href='#' onclick='OpenDig({0},1)'>未发生成本拆分</a>", item.Id) 
                //    ;
                if (string.IsNullOrEmpty(id) || id == "2")
                {
                    link += string.Format(" | <a target='_blank' href='StageSplit.aspx?id={0}'>实际已发生成本核算</a>", item.Id);
                }
                if (string.IsNullOrEmpty(id) || id == "3")
                {
                    link += string.Format(" | <a target='_blank' href='StageSplit_UnHappend.aspx?id={0}'>预计待发生成本核算</a>", item.Id);

                }
                if (string.IsNullOrEmpty(id) || id == "4")
                {
                    link += string.Format(" | <a target='_blank' href='ViewUnitCost.aspx?id={0}'>核算结果查看</a>", item.Id);
                }

            }
            content.AppendFormat(childNodeFormat, item.StageName, link);
        }
        return content.ToString();
    }
}